﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;

namespace AppBox.admin
{
    public partial class user_edit : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHidePostBackReference();

            int id = GetQueryIntValue("id");
            XUser current = XUser.FetchByID(id);
            if (current == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                ExtAspNet.Alert.Show("参数错误！", String.Empty, ExtAspNet.ActiveWindow.GetHideReference());
                return;
            }

            tbxName.Text = current.Name;
            tbxRealName.Text = current.RealName;
            tbxEmail.Text = current.Email;
            tbxCellPhone.Text = current.CellPhone;
            tbxOfficePhone.Text = current.OfficePhone;
            tbxHomePhone.Text = current.HomePhone;
            tbxRemark.Text = current.Remark;
            cbxEnabled.Checked = current.Enabled;
            ddlGender.SelectedValue = current.Gender;


            // 绑定角色下拉列表
            BindDDLRole(current);

            // 绑定部门下拉列表
            BindDDLDept(current);
        }

        private void BindDDLRole(XUser current)
        {
            XRoleCollection items = new Select().From<XRole>()
                //.OrderAsc(XRole.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<XRoleCollection>();

            ddlRole.DataTextField = "Name";
            ddlRole.DataValueField = "Id";
            ddlRole.DataSource = items;
            ddlRole.DataBind();

            ddlRole.Items.Insert(0, new ExtAspNet.ListItem("--请选择--", "-1"));
            ddlRole.SelectedValue = current.RoleId == null ? "-1" : current.RoleId.ToString();
        }

        private void BindDDLDept(XUser current)
        {
            XDeptCollection items = new Select().From<XDept>()
                .OrderAsc(XDept.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<XDeptCollection>();
            List<MyDept> mys = XDeptHelper.GetMyDeptCollection(items);

            // 绑定到下拉列表（启用模拟树功能和不可选择项功能）
            ddlDept.EnableSimulateTree = true;
            ddlDept.DataTextField = "Name";
            ddlDept.DataValueField = "Id";
            ddlDept.DataSimulateTreeLevelField = "TreeLevel";
            ddlDept.DataEnableSelectField = "Enabled";
            ddlDept.DataSource = mys;
            ddlDept.DataBind();

            ddlDept.Items.Insert(0, new ExtAspNet.ListItem("--请选择--", "-1"));
            ddlDept.SelectedValue = current.DeptId == null ? "-1" : current.DeptId.ToString();
        }
        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            int id = GetQueryIntValue("id");
            XUser item = XUser.FetchByID(id);
            item.Name = tbxName.Text.Trim();
            item.RealName = tbxRealName.Text.Trim();
            item.Gender = ddlGender.SelectedValue;
            item.Email = tbxEmail.Text.Trim();
            item.CellPhone = tbxCellPhone.Text.Trim();
            item.OfficePhone = tbxOfficePhone.Text.Trim();
            item.HomePhone = tbxHomePhone.Text.Trim();
            item.Remark = tbxRemark.Text.Trim();
            item.Enabled = cbxEnabled.Checked;
            item.RoleId = Convert.ToInt32(ddlRole.SelectedValue);
            item.DeptId = Convert.ToInt32(ddlDept.SelectedValue);
            item.Save(User.Identity.Name);

            //ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }

        #endregion

    }
}
